package com.shhic.hhplt.server.facade;

import javax.annotation.Resource;

import com.shhic.hhplt.server.biz.*;
import com.shhic.hhplt.server.exceptions.BoxNumberParamException;
import com.shhic.hhplt.server.exceptions.CodeExhaustException;
import com.shhic.hhplt.server.exceptions.NoBoxNumberException;
import com.shhic.hhplt.server.exceptions.NoSerialCodeException;
import com.shhic.hhplt.server.service.ICodePersistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shhic.hhplt.server.model.CommonRtnString;
import com.shhic.hhplt.server.model.CommonRtnValue;
import com.shhic.hhplt.server.model.NoPayload;

/**
 * 编码服务
 * 各个工位的编码都是服务端统一维护的
 * 例如MAC地址
 * @author 张文硕
 *
 */
@Controller
public class CodeServiceController {
	
	
	@Autowired
	ICodePersistService cpService;
	@Resource ISerialCodeMaintaince serialCodeMaintaince;
	@Resource IBoxNumberMaintaince boxNumberMaintaince;
	
	/**
	 * 获得服务端参数值
	 * @param key	参数名
	 * @return	参数值，发生异常或无此参数值，则返回null，由客户端调取默认值
	 */
	@RequestMapping(value = "/getParam.do", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String getParam(@RequestParam String key){
		return cpService.getParam(key);
	}
	
	/**
	 * 获得序列编码
	 * @param key	序列编码名
	 * @return
	 */
	@RequestMapping(value = "/generateSerialCode.do", method = RequestMethod.POST)
	@ResponseBody
	public CommonRtnValue<?> generateSerialCode(@RequestParam String key){
		try {
			return new CommonRtnValue<CommonRtnString>(new CommonRtnString(serialCodeMaintaince.generateSerialCode(key)));
		} catch (CodeExhaustException e) {
			e.printStackTrace();
			return new CommonRtnValue<NoPayload>(1,"序列编码["+key+"]已耗尽");
		} catch (NoSerialCodeException e) {
			return new CommonRtnValue<NoPayload>(2,"没有这个编码序列:["+key+"]");
		}
	}

	/**
	 * 获得序列编码没有回收，并且加入了箱子规则
	 * @param key	序列编码名
	 * @return
	 */
	@RequestMapping(value = "/generateSerialCodeNoRetrieved.do", method = RequestMethod.POST)
	@ResponseBody
	public CommonRtnValue<?> getSerialCode(@RequestParam String key){
		try {
			return new CommonRtnValue<CommonRtnString>(boxNumberMaintaince.generateBoxNumber(key));
		} catch (CodeExhaustException e) {
			e.printStackTrace();
			return new CommonRtnValue<NoPayload>(1,"序列编码["+key+"]已耗尽");
		}catch (BoxNumberParamException e){
			return new CommonRtnValue<NoPayload>(3,"箱号参数未配置正确");
		}catch (NoBoxNumberException e) {
			return new CommonRtnValue<NoPayload>(2,"没有这个编码序列:["+key+"]");		}
	}

	/**
	 * 获得镭雕机数量
	 * @param key 标识
	 * @return 
	 */
	@RequestMapping(value = "/getCount.do", method = RequestMethod.POST)
	@ResponseBody
	public CommonRtnValue<?> getCount(@RequestParam String key){
		try {
			return new CommonRtnValue<CommonRtnString>(boxNumberMaintaince.getCount(key));
		}catch (NoBoxNumberException e) {
			return new CommonRtnValue<NoPayload>(2,"没有这个编码序列:["+key+"]");		}
	}

	/**
	 * 回收序列编码
	 * @param key	编码名称
	 * @param code	回收值
	 * @return
	 */
	@RequestMapping(value = "/retrieveSerialCode.do", method = RequestMethod.POST)
	@ResponseBody
	public CommonRtnValue<NoPayload> retrieveSerialCode(@RequestParam String key,@RequestParam String code){
		try {
			serialCodeMaintaince.retrieveSerialCode(key,code);
			return new CommonRtnValue<NoPayload>();
		} catch (NoSerialCodeException e) {
			return new CommonRtnValue<NoPayload>(2,"没有这个编码序列:["+key+"]");
		}
	}
	
}
